#!/usr/bin/python
import os

class Utility:
    def removespostfixlash(self, string):
        return string.endswith('/') and string[:-1] or string
        
class Directory:
    def __init__(self, directory, file_type):
        self.directory = directory
        self.file_type = file_type
        self.file_list = []
        self.__setup__(self.directory, self.file_type)
        
    def __setup__(self, directory, file_type):
        try:
            for path, dirs, files in os.walk(directory):
                for filename in files:
                    file_path = os.path.join(path, filename)
                    if os.path.isfile(file_path):
                        if file_type == "*":
                            self.file_list.append(file_path)
                        elif file_path.endswith(file_type):
                            self.file_list.append(file_path)
        except:
            print "Directory object setup fail. \nDirectory = %s \nType=%s" % (directory, file_type)
    
    def __iter__(self):
        return iter(self.file_list)
    
    def count(self):
        return len(self.file_list)
    
    def percentage(self, file):
        count = len(self.file_list)
        if count <= 0:
            return str(0)
        index = self.file_list.index(file) + 1
        return str(100 * (index) / (count)) + '%'
